package com.sqnu.dashe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqnu.dashe.common.R;
import com.sqnu.dashe.entity.User;
import com.sqnu.dashe.service.UserService;
import com.sqnu.dashe.utils.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mmy
 * @create 2023-06-13 17:06
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserConroller {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;
    /*
    把验证码输出到控制台
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession httpSession) throws MessagingException {
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成四位验证码
            String code = MailUtils.achieveCode();
            //发送邮箱验证码
//            MailUtils.sendTestMail(phone,code);
            log.info("code:{}",code);

            //将生成的验证码保存在Session中
//            httpSession.setAttribute(phone,code);

            //将生成的验证码缓存到Redis中，并设置有效期为5分钟
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

            return R.success("邮箱验证码发送成功");
        }
        return R.error("验证码发送失败");
    }
    /*
    用户登录
     */
    @PostMapping("/login")
    public R<User> sendMsg(@RequestBody Map user, HttpSession httpSession){
        String phone = user.get("phone").toString();
        //获取验证码
        String code1 = user.get("code").toString();

        //从session中获取保存的验证码
//        Object attributeCode = httpSession.getAttribute(phone);

        //从Redis中获取缓存验证码
        Object attributeCode = redisTemplate.opsForValue().get(phone);

        //进行验证码比对
        if (attributeCode != null && attributeCode.equals(code1)) {
            //如果能够比对成功，说明登陆成功，
            //判断当前手机号对应的用户是否为新用户，如果为新用户，就自动完成注册
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, phone);
            User user1 = userService.getOne(queryWrapper);

            if (user1 == null) {
                user1 = new User();
                user1.setPhone(phone);
                user1.setStatus(1);
                userService.save(user1);
            }
            httpSession.setAttribute("user",user1.getId());

            //如果用户登录成功，删除Redis中缓存的验证码
            redisTemplate.delete(phone);

            return R.success(user1);

        }
        return R.error("验证码比对失败");
    }

    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }
}

